﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Media.Imaging;
using System.Xml;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class cmdLoadSelSet : IExternalCommand
	{
		public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
		{
			RibbonPanel ribbonPanel = null;
			foreach (RibbonPanel ribbonPanel2 in revit.Application.GetRibbonPanels("盈建科工具"))
			{
				if (ribbonPanel2.Name == "选择集")
				{
					ribbonPanel = ribbonPanel2;
					break;
				}
			}
			ComboBox comboBox = null;
			foreach (RibbonItem ribbonItem in ribbonPanel.GetItems())
			{
				if (ribbonItem.Name == "selList")
				{
					comboBox = (ribbonItem as ComboBox);
				}
			}
			if (comboBox != null)
			{
				foreach (ComboBoxMember comboBoxMember in comboBox.GetItems())
				{
					comboBoxMember.Visible = false;
				}
				XmlDocument xmlDocument = new XmlDocument();
				string text = Assembly.GetExecutingAssembly().Location;
				text = Path.GetDirectoryName(text);
				text = Path.GetDirectoryName(text);
				string filename = text + "\\Data\\SelSets.xml";
				xmlDocument.Load(filename);
				XmlNodeList xmlNodeList = xmlDocument.SelectNodes("SelSets/SelSet");
				string uriString = text + "\\Res\\selList.png";
				foreach (object obj in xmlNodeList)
				{
					XmlNode xmlNode = (XmlNode)obj;
					ComboBoxMember comboBoxMember2 = comboBox.AddItem(new ComboBoxMemberData(Guid.NewGuid().ToString(), xmlNode.Attributes["Text"].Value));
					comboBoxMember2.Image = new BitmapImage(new Uri(uriString, UriKind.Absolute));
					comboBox.Current = comboBoxMember2;
				}
				foreach (ComboBoxMember comboBoxMember3 in comboBox.GetItems())
				{
					if (comboBoxMember3.Visible && comboBoxMember3.ItemText == XmlInfo.currItem)
					{
						comboBox.Current = comboBoxMember3;
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
